<?


//display section pentru denumire si parinte subetapa

function display_set($idp)
{
	$selected="";
	//if($x==1)
	//{
	$i = 0; 
	$arrStages = array(); 
	$arrStagesID = array();
	
	$instructiune="SELECT stage_name, id FROM stages WHERE id_project=".$idp." order by stage_order asc;";
	$interogare=mysql_query($instructiune);
	while($row=mysql_fetch_array($interogare))
	{
		$arrStages[$i] = $row["stage_name"]; 
		$arrStagesID[$i] = $row["id"];
		$i++; 
	}
	
	$j = 0;
	$arrSubstagesP=array();
	$arrSubstages = array(); 
	$arrSubstagesID = array();
	
	$instruct="SELECT substage_name, id, id_stage FROM substages WHERE id_stage in( select id from stages where id_project=".$idp.") order by substage_order asc;";
	$interog=mysql_query($instruct);
	while($row=mysql_fetch_array($interog))
	{
		$arrSubstages[$j] = $row["substage_name"]; 
		$arrSubstagesID[$j] = $row["id"];
		$arrSubstagesP[$j]=$row["id_stage"];
		$j++; 
		echo j;
	}

	?>
	
	<script type="text/javascript"> 
		arrStage = new Array(); 
		arrStageID = new Array();
		<?php 
		for($i=0;$i<count($arrStages); $i++){ 
			echo "arrStage[$i]='".$arrStages[$i]."';\n"; 
			echo "arrStageID[$i]='".$arrStagesID[$i]."';\n";
		} 
		?>  
		arrSubstage = new Array(); 
		arrSubstageID = new Array(); 
		arrSubstageP = new Array();
		<?php 
		for($i=0;$i<count($arrSubstages); $i++){ 
			echo "arrSubstage[$i]='".$arrSubstages[$i]."';\n"; 
			echo "arrSubstageID[$i]='".$arrSubstagesID[$i]."';\n"; 
			echo "arrSubstageP[$i]='".$arrSubstagesP[$i]."';\n"; 
		} 
		?> 
		

	function umple(idx) {
	
		var f=document.adaugasubet;
		
		f.sel_place.options.length=null;
			var j=1;
			f.sel_place.options[0] = new Option(">-- Place first --<",0);
			for(var i=0; i<arrSubstage.length; i++) 
			{
				if (arrSubstageP[i]==idx) 
				{
					f.sel_place.options[j] = new Option(arrSubstage[i],arrSubstageID[i]);
					j++;
					
				} 
			}
			f.sel_place.options[0].selected = true; 
		
	}
	</script>
			
			
	?> <form action="" name="adaugasubet" method="post" >
    <table width="100%">
    <tr><td> Substage name: </td> <td><input name="subname" type="text" value="" style="width:98%"></td></tr>
    <tr><td> Substage parent: </td><td>
			<select name="stage_parent" onChange="umple(this.value)" style="width:250px;">
 			<?php
					echo "<option>-- Select Item --</option>\n";
					$instructiune="SELECT stage_name, id FROM stages WHERE id_project=".$idp." order by stage_order asc;";
					$interogare=mysql_query($instructiune);
					while($rand=mysql_fetch_array($interogare))
						{
							if($_GET["stage_parent"]==$rand['id']) $selected="selected";
								else $selected="";
							echo("\t   <option {".$selected."} value='".$rand['id']."'> ".$rand['stage_name']." </option>  \n");
					
						}
					echo("\t   <option {".$selected."} value='0'> 'pozitioneaza ca ultima etapa' </option>  \n");
			?>            
			</select>
		</td>
        </tr>
		   <tr><td>Place after</td><td><select name="sel_place" style="width:100%"></select></td></tr>
           
           <tr> <td> </td> <td> <input type="submit" name="aplica" value="Apply" align="right"> </td></tr>
           </table>
           </form>
  <? }
  
/*  
function displaysub($sub, $parent, $idp)
{
	?><form action="project_stages.php?rowid=<? echo $idp?>&amp;subn=<? echo $sub?>&amp;par=<? echo $parent?>" name="adauga" method="post" >
           <table>
           </td></tr> 
            <tr><td> Pozitionare dupa: </td><td>
    	<form  action="project_stages.php" method="get">
        
			<select name="substage_pos" onchange="" style="width:250px;">
 			<?php
    		echo "<option>-- Select Item --</option>\n";
			$instructiune2="SELECT substage_name, id FROM substages WHERE id_stage=".$parent." order by substage_order asc;";
			$interogare2=mysql_query($instructiune2);
			while($rand2=mysql_fetch_array($interogare2))
				{
					if($_GET["substage_pos"]==$rand2['id']) $selected="selected";
						else $selected="";
					echo("\t   <option {".$selected."} value='".$rand2['id']."'> ".$rand2['substage_name']." </option>  \n");
			
				}
			echo("\t   <option {".$selected."} value='0'> 'pozitioneaza ca prima subetapa' </option>  \n");
			?>
            
			</select>
           <!-- </form> -->
            </form> </td></tr>
        <tr> <td> </td> <td> <input type="submit" name="add_se" value="Adauga" align="right"> </td></tr>
        </table>
           
	</form>

 
    <?
 
	?>

	
 <?	
   
}
*/


function display($idp)
{
	//if($x==1)
	//{
	?> <form action="" name="adauga" method="post" >
    <table width="100%">
    <tr><td> Denumire etapa: </td> <td><input name="name" type="text" value="" style="width:98%"></td></tr>
    <tr><td> Pozitionare dupa: </td><td>
    	<form  action="project_stages.php" method="get">
			<select name="stage" onchange="" style="width:250px;">
 			<?php
    		echo "<option>-- Select Item --</option>\n";
			$instructiune="SELECT stage_name, id FROM stages WHERE id_project=".$idp." order by stage_order asc;";
			$interogare=mysql_query($instructiune);
			while($rand=mysql_fetch_array($interogare))
				{
					if($_GET["stage"]==$rand['id']) $selected="selected";
						else $selected="";
					echo("\t   <option {".$selected."} value='".$rand['id']."'> ".$rand['stage_name']." </option>  \n");
			
				}
			echo("\t   <option {".$selected."} value='0'> 'pozitioneaza ca prima etapa' </option>  \n");
		
			?>
            
			</select>
          
            </form> </td></tr>
        <tr> <td> </td> <td> <input type="submit" name="add" value="Adauga" align="right"> </td></tr>
        </table>
	</form>
  	
 <?	
   
}

	function refreshPage($destination)
	{
		print "<meta http-equiv=\"refresh\" content=\"0; url=".$destination."\">";
	 
	}

//Connect to db:
function dbConn()
{
$username = "root";
$password = "";
$hostname = "localhost";	
$handler = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
//print "Connected to MySQL<br>";

if (!mysql_select_db('ec_methodology', $handler)) {
    echo 'Could not select database';
    exit;
}
return $handler;
}

//DB Close:
function dbClose($handler)
{
	mysql_close($handler)
	or die("Error closing connection. Was the connection open before?");
	//echo "conexiune inchisa cu succes";
}

//query:
function returnQuery($qr,$handler)
{
//SELECT * FROM `projects` WHERE `ID` LIKE '1'
$result = mysql_query($qr);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query. "\n";
    die($message);
	//de adaugat in continuare cum se afiseaza tabelul!!!
}
return $result;
}
?>